package com.chee.plant.logic.user;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.json.JSONException;
import org.json.JSONObject;

import com.chee.plant.hibernate.dao.PlantLogDAO;
import com.chee.plant.hibernate.object.PlantLog;
import com.chee.plant.logic.CmdHandler;
import com.chee.plant.logic.PlantCmd;
import com.chee.plant.server.Message;

/**
 * 显示用户所有日志
 * 
 * @author xue cb
 * 
 */
public class ShowUserLogHandler implements CmdHandler {

	@Override
	public Vector<Message> executeCmd(Message msg) {
		// 要返回的内容
		Vector<Message> _msgs = new Vector<Message>();
		// 解析客户端传过来的内容
		int userId = msg.getUserId();
		// 从数据库中查询用户日志
		List<PlantLog> list = new PlantLogDAO().getLogByUserId(userId);
		int result = 1;
		Message ret = new Message(userId, PlantCmd.GetUserLog, result);
		List<JSONObject> logList = new ArrayList<JSONObject>();
		Iterator<PlantLog> iterator = list.iterator();
		while (iterator.hasNext()) {
			PlantLog plantLog = iterator.next();
			JSONObject obj = new JSONObject();
			try {
				obj.append("id", plantLog.getId());
				obj.append("user_id", plantLog.getUserId());
				obj.append("desc", plantLog.getDescription());
				obj.append("create_time", plantLog.getCreateTime());
			} catch (JSONException e) {
				// 添加Json格式错误处理
				e.printStackTrace();
			}
			logList.add(obj);
		}
		ret.addVariable("list", logList);
		_msgs.add(ret);
		return _msgs;

	}

	public static void main(String[] args) {

	}

}
